package code

import (
	"strings"
)

// MaskData 脱敏，1姓名，2手机号，3身份证号，4Email，5银行卡
func MaskData(oldStr string, kind uint8) string {
	if oldStr == "" {
		return ""
	}
	var newStr string
	switch kind {
	case 1:
		// 人名脱敏
		strArr := make([]string, 0)
		for _, char := range oldStr {
			strArr = append(strArr, string(char))
		}
		if len(strArr) < 3 {
			newStr += strArr[0] + "*"
		} else {
			newStr += strArr[0] + "*" + strArr[len(strArr)-1]
		}
	case 2:
		// 手机号脱敏
		newStr = oldStr[0:3] + "****" + oldStr[7:]
	case 3:
		// 身份证号脱敏
		newStr = oldStr[0:6] + "********" + oldStr[14:]
	case 4:
		// 电子邮箱地址脱敏
		idx := strings.Index(oldStr, "@")
		newStr = oldStr[0:1] + "****" + oldStr[idx-1:]
	case 5:
		// 银行卡脱敏
		newStr = oldStr[0:2] + "********" + oldStr[len(oldStr)-2:]
	}
	return newStr
}

// MaskData2 脱敏，1姓名，2手机号，3身份证号，4Email，5银行卡
func MaskData2(oldStr string, kind uint8) string {
	if oldStr == "" {
		return ""
	}
	var newStr string
	switch kind {
	case 1:
		// 人名脱敏
		strArr := make([]string, 0)
		for _, char := range oldStr {
			strArr = append(strArr, string(char))
		}
		if len(strArr) < 3 {
			for i := 0; i < len(strArr); i++ {
				if i == 0 {
					newStr += strArr[i]
				} else {
					newStr += "*"
				}
			}
		} else {
			for i := 0; i < len(strArr); i++ {
				if i == 0 {
					newStr += strArr[i]
				} else if i == len(strArr)-1 {
					newStr += strArr[i]
				} else {
					newStr += "*"
				}
			}
		}
	case 2:
		// 手机号脱敏
		newStr = oldStr[0:3] + "****" + oldStr[7:]
	case 3:
		// 身份证号脱敏
		newStr = oldStr[0:6] + "********" + oldStr[14:]
	case 4:
		// 电子邮箱地址脱敏
		idx := strings.Index(oldStr, "@")
		newStr = oldStr[0:1]
		for i := 0; i < len(oldStr); i++ {
			if i < len(oldStr[:idx])-2 {
				newStr += "*"
			}
		}
		newStr += oldStr[idx-1:]
	case 5:
		// 银行卡脱敏
		newStr = oldStr[0:2]
		for i := 0; i < len(oldStr); i++ {
			if i < len(oldStr)-4 {
				newStr += "*"
			}
		}
		newStr += oldStr[len(oldStr)-2:]
	}
	return newStr
}
